package com.mcptt.http.auth.process;

import java.net.URI;

import com.cloudptt.api.product.log.Log;
import com.mcptt.config.callback.IAuthCallback;
import com.mcptt.http.auth.process.digest.HttpDigestSubProcessor;
import com.mcptt.http.auth.process.passwd.HttpPasswdSubProcessor;
import com.mcptt.http.auth.process.result.FinalResult;

public class HttpProcessor {

	private HttpSubProcessor processor = null;
	public static final int Type_Acr_Password = 1;
	public static final int Type_Acr_Digest = 2;

	public HttpProcessor(int type, URI authUri, URI loginUri, URI tokenUri) {
		if (Type_Acr_Digest == type) {
			this.processor = new HttpDigestSubProcessor(authUri, loginUri, tokenUri);
		} else {
			this.processor = new HttpPasswdSubProcessor(authUri, loginUri, tokenUri);
		}
	}


	public void runAuthorizationThread(final String clientId, final String redirectUri, final String mcId, final String pwd, IAuthCallback callback) {
		Runnable runnable = new Runnable() {
			@Override
			public void run() {
				// TODO: http request.
				try {
					processor.doAuthorization(clientId, redirectUri, mcId, pwd, callback);
				} catch (Exception ex) {
					// TODO Auto-generated catch block
					ex.printStackTrace();
					Exception e = new HttpAuthFailedException("run", ex.toString());
					processor.sendAuthResultMsg(HttpPasswdSubProcessor.AUTH_FAILED, e);
				}
			}
		};
		new Thread(runnable).start();
	}


	public void runRefreshTokenThread(String refreshToken, IAuthCallback callback) {
		Runnable runnable = new Runnable() {
			@Override
			public void run() {
				try {
					processor.doRefreshToken(refreshToken, callback);
				} catch (Exception ex) {
					ex.printStackTrace();
					Exception e = new HttpAuthFailedException("run", ex.toString());
					processor.sendAuthResultMsg(HttpPasswdSubProcessor.REFRESH_FAILED, e);
				}
			}
		};
		new Thread(runnable).start();
	}
}